<template>
  <div id="app">
    <el-config-provider :locale="zhCn">
      <el-container class="layout-container">
        <el-aside class="main-aside" width="220px">
          <div class="aside-logo">
            <el-icon style="font-size:32px;"><Film /></el-icon>
            <span class="aside-title">电影推荐系统</span>
          </div>
          <el-menu
            :router="true"
            default-active="$route.path"
            class="aside-menu"
            background-color="#fff"
            text-color="#222"
            active-text-color="#1565c0"
            :unique-opened="true"
            :collapse="false"
            mode="vertical"
          >
            <el-menu-item index="/movies">
              <el-icon><Film /></el-icon>
              <span>电影管理</span>
            </el-menu-item>
            <el-menu-item index="/coming-soon">
              <el-icon><Film /></el-icon>
              <span>待映电影</span>
            </el-menu-item>
            <el-menu-item index="/users">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/about">
              <el-icon><InfoFilled /></el-icon>
              <span>关于</span>
            </el-menu-item>
          </el-menu>
          <div v-if="isLoggedIn" class="aside-user">
            <el-avatar :size="36" :icon="UserFilled" />
            <span class="aside-username">{{ username }}</span>
            <el-button type="text" @click="handleLogout" class="aside-logout">
              <el-icon><SwitchButton /></el-icon>
            </el-button>
          </div>
        </el-aside>
        <el-container>
          <el-main>
            <router-view />
          </el-main>
        </el-container>
      </el-container>
    </el-config-provider>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElConfigProvider } from 'element-plus'
import { 
  SwitchButton, 
  User, 
  Film, 
  InfoFilled,
  UserFilled
} from '@element-plus/icons-vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'

const router = useRouter()
const isLoggedIn = ref(false)
const username = ref('')

onMounted(() => {
  isLoggedIn.value = localStorage.getItem('isLoggedIn') === 'true'
  username.value = localStorage.getItem('username') || ''
  window.addEventListener('login-success', () => {
    isLoggedIn.value = true
    username.value = localStorage.getItem('username') || ''
  })
})

const handleLogout = () => {
  localStorage.removeItem('isLoggedIn')
  localStorage.removeItem('username')
  isLoggedIn.value = false
  router.push('/login')
}
</script>

<style>
html, body, #app {
  width: 100%;
  min-width: 0;
  font-size: 16px;
  box-sizing: border-box;
}

#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  min-height: 100vh;
}

body {
  margin: 0;
  padding: 0;
  background-color: #f5f7fa;
}

.layout-container {
  min-height: 100vh;
  width: 100%;
  padding-left: 220px;
}

.el-container {
  width: 100%;
}

.main-header {
  background: white;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 0;
  position: fixed;
  width: 100%;
  z-index: 1000;
}

.header-content {
  max-width: 1800px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.logo {
  margin-right: 40px;
}

.logo h1 {
  margin: 0;
  font-size: 1.5em;
  color: #409EFF;
  font-weight: 600;
}

.main-menu {
  flex: 1;
  border-bottom: none;
}

.user-actions {
  margin-left: 20px;
}

.user-dropdown {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-dropdown:hover {
  background-color: #f5f7fa;
}

.username {
  margin-left: 8px;
  font-size: 14px;
  color: #606266;
}

.el-main {
  padding-top: 80px;
  max-width: 1800px;
  margin: 0 auto;
}

:deep(.el-menu--horizontal) {
  border-bottom: none;
}

:deep(.el-menu-item) {
  font-size: 15px;
}

:deep(.el-menu-item .el-icon) {
  margin-right: 4px;
}

.el-aside, .main-aside {
  position: fixed;
  left: 0;
  top: 0;
  height: 100vh;
  width: 220px;
  background: #fff;
  box-shadow: 2px 0 12px 0 rgba(0,0,0,0.04);
  display: flex;
  flex-direction: column;
  z-index: 100;
}
.aside-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 0 18px 0;
  border-bottom: 1px solid #f0f0f0;
}
.aside-title {
  margin-top: 10px;
  font-size: 1.25em;
  color: #1565c0;
  font-weight: 700;
  letter-spacing: 1px;
}
.aside-menu {
  flex: 1;
  margin-top: 18px;
  border-right: none;
  min-height: 0;
}
.aside-menu .el-menu-item {
  height: 48px;
  font-size: 16px;
  border-radius: 8px;
  margin: 0 10px 8px 10px;
  display: flex;
  align-items: center;
}
.aside-menu .el-menu-item.is-active {
  background: #e3f0fd;
  color: #1565c0;
}
.aside-menu .el-menu-item .el-icon {
  margin-right: 8px;
}
.aside-user {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 18px 0 18px 0;
  border-top: 1px solid #f0f0f0;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: #fff;
}
.aside-username {
  margin-top: 8px;
  color: #1565c0;
  font-size: 15px;
  font-weight: 600;
}
.aside-logout {
  margin-top: 8px;
  color: #888;
}
.aside-logout:hover {
  color: #f56c6c;
}
</style>
